<template>
  <el-form ref="form" :model="form" label-width="120px">
    <el-form-item label="文章分类: ">
      <el-select v-model="form.categoryId">
        <el-option
          v-for="item in data.category"
          :key="item.id"
          :value="item.id"
          :label="item.category_name"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="文章标题: ">
      <el-input v-model="form.title"></el-input>
    </el-form-item>
    <el-form-item label="缩略图: ">
      <UploadImg imgUrl.sync="form.imgUrl" :config="uploadImgConfig" />
    </el-form-item>
    <el-form-item label="发布日期: ">
      <el-date-picker
        v-model="form.createdAt"
        type="date"
        placeholder="选择日期"
        disabled
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="详细内容: ">
      <quill-editor v-model="form.content"></quill-editor>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit" :loading="data.submitLoading">保存</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { reactive, ref, watch, onMounted } from "@vue/composition-api";
import { editArticle, getArticleList } from "../../api/article";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import UploadImg from "../../components/upload";
import { quillEditor } from "vue-quill-editor";
export default {
  components: {
    quillEditor,
    UploadImg
  },
  setup(props, { root }) {
    /** === === === === === === data === === === === === ===*/
    //#region
    const data = reactive({
      id: root.$route.params.id || root.$store.state.articleDetail.id,
      category: [],
      editorOption: {},
      submitLoading: false,
      uploadKey: {
        token: "",
        key: ""
      }
    });
    const form = reactive({
      categoryId: "",
      title: "",
      createdAt: "",
      content: "",
      imgUrl: ""
    });
    // 图片上传配置
    const uploadImgConfig = reactive({
      action: "",
      bucket: ""
    });
    //#endregion

    /** === === === === === === method === === === === === ===*/
    //#region
    const getCategory = () => {};
    const getInfo = () => {
      const requestData = {
        pageNumber: 1,
        pageSize: 1,
        id: data.id
      };
      getArticleList(requestData).then(res => {
        data.form = {
          categoryId: res.categoryId,
          title: res.title,
          createdAt: res.createdAt,
          content: res.content,
          imgUrl: res.imgUrl
        };
      });
    };
    const submit = () => {
      data.submitLoading = true;
      const requestData = {
        id: data.id,
        category: form.category,
        title: form.title,
        content: form.content,
        imgUrl: form.imgUrl
      };

      //todo 检验各字段的值是否合法

      editArticle(requestData)
        .then(data => {
          root.$message({
            message: "添加成功"
          });
          data.submitLoading = false;
        })
        .catch(err => {
          data.submitLoading = false;
        });
    };
    //#endregion

    onMounted(() => {
      getCategory();
      getInfo();
    });

    return {
      data,
      form,
      uploadImgConfig,
      submit
    };
  }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
